﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Parser.MarkupRewriter
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using System.Collections.Generic;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;

namespace System.Web.Razor.Parser
{
  internal abstract class MarkupRewriter : ParserVisitor, ISyntaxTreeRewriter
  {
    private Stack<BlockBuilder> _blocks = new Stack<BlockBuilder>();
    private Action<SpanBuilder, SourceLocation, string> _markupSpanFactory;

    protected MarkupRewriter(
      Action<SpanBuilder, SourceLocation, string> markupSpanFactory)
    {
      this._markupSpanFactory = markupSpanFactory != null ? markupSpanFactory : throw new ArgumentNullException(nameof (markupSpanFactory));
    }

    protected BlockBuilder Parent => this._blocks.Count <= 0 ? (BlockBuilder) null : this._blocks.Peek();

    public virtual System.Web.Razor.Parser.SyntaxTree.Block Rewrite(System.Web.Razor.Parser.SyntaxTree.Block input)
    {
      input.Accept((ParserVisitor) this);
      return this._blocks.Pop().Build();
    }

    public override void VisitBlock(System.Web.Razor.Parser.SyntaxTree.Block block)
    {
      if (this.CanRewrite(block))
      {
        SyntaxTreeNode syntaxTreeNode = this.RewriteBlock(this._blocks.Peek(), block);
        if (syntaxTreeNode == null)
          return;
        this._blocks.Peek().Children.Add(syntaxTreeNode);
      }
      else
      {
        BlockBuilder blockBuilder = new BlockBuilder(block);
        blockBuilder.Children.Clear();
        this._blocks.Push(blockBuilder);
        base.VisitBlock(block);
        if (this._blocks.Count <= 1)
          return;
        this._blocks.Pop();
        this._blocks.Peek().Children.Add((SyntaxTreeNode) blockBuilder.Build());
      }
    }

    public override void VisitSpan(Span span)
    {
      if (this.CanRewrite(span))
      {
        SyntaxTreeNode syntaxTreeNode = this.RewriteSpan(this._blocks.Peek(), span);
        if (syntaxTreeNode == null)
          return;
        this._blocks.Peek().Children.Add(syntaxTreeNode);
      }
      else
        this._blocks.Peek().Children.Add((SyntaxTreeNode) span);
    }

    protected virtual bool CanRewrite(System.Web.Razor.Parser.SyntaxTree.Block block) => false;

    protected virtual bool CanRewrite(Span span) => false;

    protected virtual SyntaxTreeNode RewriteBlock(BlockBuilder parent, System.Web.Razor.Parser.SyntaxTree.Block block) => throw new NotImplementedException();

    protected virtual SyntaxTreeNode RewriteSpan(BlockBuilder parent, Span span) => throw new NotImplementedException();

    protected void FillSpan(SpanBuilder builder, SourceLocation start, string content) => this._markupSpanFactory(builder, start, content);
  }
}
